Hi all,
Recently started using the API integration to generate incidents. The process is pretty simple:
- [Custom code] Get a list of stuff that has issues (each item has an unique identifier used as the incident_key)
- [API code] Get a list of all incidents (id+incident_key) using
GET https://api.pagerduty.com/incidents
- [API code] For any incident_key in 1 but not in 2 create new incident using
POST https://api.pagerduty.com/incidents
The problem I am seeing is after running this for a bit I start to get duplicate incident ids from 2 and missing incident_keys which manifest in Open incident with matching dedup key already exists on this service
Here is a snippet the python code I am using to get a list of all incidents:
more = True
offset = 0
limit = 100
while more:
querystring = {"service_ids[]": "something", "offset": offset, "limit": limit}
resp = requests.request("GET", "https://api.pagerduty.com/incidents", headers=headers, params=querystring)
if resp.status_code != 200:
exit(1)
resp_json = resp.json()
for i in resp_json["incidents"]:
if i["status"] != "resolved":
if i["incident_key"] in ret:
print("Duplicate id {} {}".format(i["id"], i["incident_key"]))
else:
ret[i["incident_key"]] = i["id"]
more = resp_json["more"]
offset = offset + limit
The service dashboard shows N incidents and this code returns less than N incidents because at least one has a duplicate id. The duplicates have an identical id and incident_key, so its like one is just missing. The total number of iterations does match the N dashboard incidents.
The big problem comes from the fact that when I list all the incidents incident_key X does not exists, but when I create a incident using X I get the error saying it does.
Code snippet for creation of incidents:
payload = {
"incident": {
"type": "incident",
"incident_key": "some unique id",
"title": "something bad",
"service": {
"id": "something",
"type": "service_reference"
},
"body": {
"type": "incident_body",
"details": "custom details"
}
}
}
requests.post("https://api.pagerduty.com/incidents", data=json.dumps(payload), headers=headers)